﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.PointCloud.Editing.CApPOCEditManagerApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.ModelObjects;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.PointCloud.Editing;

internal class CApPOCEditManagerApp
{
  private static TxPointCloud m_pointCloudInEdit;
  private static TxPointCloudSelectionTypeEx m_currentSelectionType;

  public static void toggleSelectByRectangleMode()
  {
    try
    {
      TxPointCloudRootEx.SelectionType = TxPointCloudRootEx.SelectionType != null ? (TxPointCloudSelectionTypeEx) 0 : (TxPointCloudSelectionTypeEx) 1;
      CApPOCEditManagerApp.HandleCursorMode();
    }
    catch (TxPointCloudNotSupportedException ex)
    {
      int num = (int) TxMessageBox.Show(PointCloudStringTable.POINT_CLOUD_NOT_SUPPORTED, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
  }

  public static bool isSelectByRectangleActive()
  {
    return CApPOCEditManagerApp.m_currentSelectionType == 1;
  }

  public static ITxObject getSelectedIfSingleSelection()
  {
    ITxObject ifSingleSelection = (ITxObject) null;
    if (TxApplication.ActiveSelection.Count == 1)
      ifSingleSelection = TxApplication.ActiveSelection.GetLastPickedItem();
    return ifSingleSelection;
  }

  public static bool CanPointsBeMovedToSelection()
  {
    bool selection = false;
    ITxObject ifSingleSelection = CApPOCEditManagerApp.getSelectedIfSingleSelection();
    switch (ifSingleSelection)
    {
      case ITxPointCloud _:
        selection = TxPointCloud.op_Equality(CApPOCEditManagerApp.m_pointCloudInEdit, ifSingleSelection);
        break;
      case ITxPointCloudLayer _:
        ITxObjectCollection collection = ((ITxObject) (ifSingleSelection as ITxPointCloudLayer)).Collection;
        if (collection is ITxPointCloud)
        {
          selection = TxPointCloud.op_Equality(CApPOCEditManagerApp.m_pointCloudInEdit, (ITxObject) collection);
          break;
        }
        break;
    }
    return selection;
  }

  public static bool EndEditPointCloud()
  {
    bool flag = false;
    if (CApPOCEditManagerApp.IsPointCloudInEdit)
    {
      CApPOCEditManagerApp.StartPointCloudEdit();
      try
      {
        flag = TxPointCloudRootEx.UnsetPointCloudEditing();
      }
      catch (TxPointCloudNotSupportedException ex)
      {
        int num = (int) TxMessageBox.Show(PointCloudStringTable.POINT_CLOUD_NOT_SUPPORTED, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      }
      CApPOCEditManagerApp.EndPointCloudEdit();
    }
    return flag;
  }

  public static bool IsNonEmptyPointsSelection => CApPOCEditManagerApp.IsPointCloudInEdit;

  public static void ClearPointsSelection()
  {
    if (!CApPOCEditManagerApp.IsNonEmptyPointsSelection)
      return;
    try
    {
      TxPointCloudRootEx.UnselectAllPointCloudPoints();
    }
    catch (TxPointCloudNotSupportedException ex)
    {
      int num = (int) TxMessageBox.Show(PointCloudStringTable.POINT_CLOUD_NOT_SUPPORTED, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
  }

  private static void RegisterPointCloudInEditEvent()
  {
    if (!TxPointCloud.op_Inequality(CApPOCEditManagerApp.m_pointCloudInEdit, (ITxObject) null))
      return;
    CApPOCEditManagerApp.m_pointCloudInEdit.EditStateChanged += new TxPointCloud_OnEditStateChangedEventHandler(CApPOCEditManagerApp.CApPOCEditManager_OnEditStateChanged);
    CApPOCEditManagerApp.RegisterToUnloadDocumentEvent();
  }

  private static void UnRegisterCurrenttPointCloudInEditEvent()
  {
    if (!TxPointCloud.op_Inequality(CApPOCEditManagerApp.m_pointCloudInEdit, (ITxObject) null))
      return;
    CApPOCEditManagerApp.m_pointCloudInEdit.EditStateChanged -= new TxPointCloud_OnEditStateChangedEventHandler(CApPOCEditManagerApp.CApPOCEditManager_OnEditStateChanged);
    CApPOCEditManagerApp.UnRegisterFromUnloadDocumentEvent();
  }

  private static void CApPOCEditManager_OnEditStateChanged(
    object sender,
    TxPointCloud_EditStateChangedEventArgs editState)
  {
    ITxPointCloud pointCloud = (ITxPointCloud) null;
    if (TxPointCloudRootEx.IsPointCloudInEditing())
      pointCloud = TxPointCloudRootEx.GetPointCloudInEditing();
    CApPOCEditManagerApp.handleChangePointCloudInEdit(pointCloud, true);
  }

  private static void handleChangePointCloudInEdit(ITxPointCloud pointCloud, bool fromEvent)
  {
    try
    {
      if (pointCloud == CApPOCEditManagerApp.m_pointCloudInEdit)
        return;
      if (TxPointCloud.op_Inequality(CApPOCEditManagerApp.m_pointCloudInEdit, (ITxObject) null))
        CApPOCEditManagerApp.UnRegisterCurrenttPointCloudInEditEvent();
      if (!fromEvent)
      {
        if (pointCloud != null)
          TxPointCloudRootEx.SetPointCloudToEditing(pointCloud);
        else
          TxPointCloudRootEx.UnsetPointCloudEditing();
      }
      CApPOCEditManagerApp.m_pointCloudInEdit = pointCloud as TxPointCloud;
      if (TxPointCloud.op_Inequality(CApPOCEditManagerApp.m_pointCloudInEdit, (ITxObject) null))
        CApPOCEditManagerApp.RegisterPointCloudInEditEvent();
      CApPOCEditManagerApp.handleApplicationMode();
    }
    catch (TxPointCloudNotSupportedException ex)
    {
      int num = (int) TxMessageBox.Show(PointCloudStringTable.POINT_CLOUD_NOT_SUPPORTED, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
  }

  private static void handleApplicationMode()
  {
    if (TxPointCloud.op_Equality(CApPOCEditManagerApp.m_pointCloudInEdit, (ITxObject) null))
      TxPointCloudRootEx.SelectionType = (TxPointCloudSelectionTypeEx) 0;
    CApPOCEditManagerApp.HandleCursorMode();
  }

  private static void HandleCursorMode()
  {
    TxPointCloudSelectionTypeEx selectionType = TxPointCloudRootEx.SelectionType;
    if (selectionType == CApPOCEditManagerApp.m_currentSelectionType)
      return;
    CApPOCEditManagerApp.m_currentSelectionType = selectionType;
    new TxCommandManager().ExecuteCommandByCommandProgId("GraphicViewerModes.ModePOCSelectByRectangleCmd.1", (object) null, false);
  }

  private static void RegisterToUnloadDocumentEvent()
  {
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(CApPOCEditManagerApp.OnUnloadDocument);
  }

  private static void UnRegisterFromUnloadDocumentEvent()
  {
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(CApPOCEditManagerApp.OnUnloadDocument);
  }

  private static void OnUnloadDocument(object sender, TxDocument_UnloadingEventArgs args)
  {
    if (!TxPointCloudRootEx.IsPointCloudInEditing())
      return;
    CApPOCEditManagerApp.PointCloudInEdit = (ITxPointCloud) null;
  }

  public static ITxPointCloud PointCloudInEdit
  {
    set
    {
      if (value == CApPOCEditManagerApp.m_pointCloudInEdit)
        return;
      CApPOCEditManagerApp.StartPointCloudEdit();
      CApPOCEditManagerApp.handleChangePointCloudInEdit(value, false);
      CApPOCEditManagerApp.EndPointCloudEdit();
    }
    get => (ITxPointCloud) CApPOCEditManagerApp.m_pointCloudInEdit;
  }

  public static bool IsPointCloudInEdit
  {
    get => TxPointCloud.op_Inequality(CApPOCEditManagerApp.m_pointCloudInEdit, (ITxObject) null);
  }

  public static void StartPointCloudEdit() => Cursor.Current = Cursors.WaitCursor;

  public static void EndPointCloudEdit()
  {
    TxApplication.ActiveUndoManager.ClearAllTransactions();
    Cursor.Current = Cursors.Default;
  }

  public static void MoveSelectedPointCloudPointsToPointCloudLayer()
  {
    ITxObject ifSingleSelection = CApPOCEditManagerApp.getSelectedIfSingleSelection();
    switch (ifSingleSelection)
    {
      case ITxPointCloudLayer _:
      case ITxPointCloud _:
        TxPerformanceMeasurementEx.StartMeasureTask("Move Points");
        CApPOCEditManagerApp.StartPointCloudEdit();
        try
        {
          TxPointCloudRootEx.MoveSelectedPointCloudPointsToPointCloudLayer(ifSingleSelection);
          TxApplication.RefreshDisplay();
        }
        catch (TxPointCloudNotSupportedException ex)
        {
          int num = (int) TxMessageBox.Show(PointCloudStringTable.POINT_CLOUD_NOT_SUPPORTED, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }
        CApPOCEditManagerApp.EndPointCloudEdit();
        TxPerformanceMeasurementEx.StopMeasureTask("Move Points");
        break;
    }
  }
}
